<?php
/**
 * Created by PhpStorm.
 * User: 26243
 * Date: 2017/5/5
 * Time: 17:45
 */
require_once '../include.php';
session_start();
$productid = intval($_POST['productid']);
//var_dump($productid);
$num = intval($_POST['num']);
//var_dump($num);
$userid = $_SESSION['loginFlag'];
//var_dump($userid);
    $sql = "select * from imooc_pro WHERE id={$productid}";
    $rows= fetchAll($sql);
    //$price=$rows['iPrice'];
    //var_dump($price);
if ($rows && is_array($rows)):
    foreach ($rows as $row):
        $price = $row['iPrice'];
    endforeach;
    endif;

//购物车添加数据

//购物车添加数据
$sql = "select * from shop_cart WHERE productid={$productid} AND userid={$userid}";
$rows= fetchAll($sql);
if($rows){
    global $link;
    //$table = 'shop_cart';
    //$arr = array($productid,$userid,$num);
    $sql = "update shop_cart set num=$num+num WHERE productid={$productid} AND userid={$userid}";
    $result = mysqli_query($link,$sql);
}else{
    $createtime = time();
    $arr = array($productid,$userid,$num,$price,$createtime);
    global $link;
    $vals = "'" . join("','", array_values($arr)) . "'";
    $sql = "insert into shop_cart(productid,userid,num,price,createtime) VALUES({$vals})";
    $result=mysqli_query($link, $sql);
}
if ($result) {
    $response = array(
        'errno' => 0,
        'errmsg' => '添加成功',
        'data' => true,
    );
}else{
    $response = array(
        'errno' => -1,
        'errmsg' => '添加失败',
        'data' => flase,
    );
}

echo json_encode($response);